Skip to content

Conversation

CYFS3
Copy link
Contributor

@CYFS3 CYFS3 commented Jul 16, 2025

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

为WCH RISC系列适配串口V2驱动

你的解决方案是什么 (what is your solution)

目前已经使用测试代码,在ch32v307v-r1测试

#include <board.h>
#include <rtthread.h>
#include <rtdevice.h>

#define echo_test_buffer_size (1024)
// 测试环境
// 串口2、3开启dma, 5、6不开启
// 缓冲区大小都为 128 字节

// echo test
// 3tx - 6rx
// 3rx - 6tx

// self test
// 2tx - 2rx
// 5tx - 5rx

static rt_device_t u2serial;
static rt_device_t u3serial;
static rt_device_t u5serial;
static rt_device_t u6serial;

static rt_uint32_t u2rx_length = 0;
static rt_uint32_t u2tx_length = 0;

static rt_uint32_t u3rx_length = 0;
static rt_uint32_t u3tx_length = 0;

static rt_uint32_t u5rx_length = 0;
static rt_uint32_t u5tx_length = 0;

static rt_uint32_t u6rx_length = 0;
static rt_uint32_t u6tx_length = 0;

static void echo_test_u3_thread_entry(void *parameter)
{
    char *uart_name = "uart3";
    static char rx_buffer[echo_test_buffer_size + 1];

    /* 查找串口设备 */
    u3serial = rt_device_find(uart_name);
    if (!u3serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }

    rt_device_open(u3serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_ssize_t buf_datalen = 0;
    while (1)
    {
        rt_device_control(u3serial, RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT, (void *)&buf_datalen);
        int32_t recbLen = rt_device_read(u3serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
        if (recbLen > 0)
        {
            u3rx_length += recbLen;
            u3tx_length += rt_device_write(u3serial, 0, rx_buffer, recbLen);
        }
    }
}

static void echo_test_u6_thread_entry(void *parameter)
{
    static char rx_buffer[echo_test_buffer_size + 1];
    rt_ssize_t buf_datalen = 0;
    while (1)
    {
        rt_device_control(u6serial, RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT, (void *)&buf_datalen);
        int32_t recbLen = rt_device_read(u6serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
        if (recbLen > 0)
        {
            u6rx_length += recbLen;
        }
    }
}

static void echo_test(void *parameter)
{
    static char tx_buffer[echo_test_buffer_size];

    for (uint32_t i = 0; i < sizeof(tx_buffer); i++)
        tx_buffer[i] = i;

    char *uart_name = "uart6";
    u6serial = rt_device_find(uart_name);
    if (!u6serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }

    rt_device_open(u6serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_thread_startup(rt_thread_create("serial3", echo_test_u3_thread_entry, RT_NULL, 2048, 7, 5));
    rt_thread_startup(rt_thread_create("serial6", echo_test_u6_thread_entry, RT_NULL, 2048, 7, 5));

    uint32_t count = 0;
    uint32_t sendTotalCount = 0;
    while (1)
    {
        count++;

        // 不定长发送数据
        uint32_t sendCount = rand() % echo_test_buffer_size;
        u6tx_length += rt_device_write(u6serial, 0, tx_buffer, sendCount);
        sendTotalCount += sendCount;

        // 等待交叉发送完毕
        rt_thread_mdelay(15);

        if (count % 100 == 0)
        {
            srand(rt_tick_get());
            rt_kprintf("echo, uart3: tx: %ld, rx: %ld\r\n", u3tx_length, u3rx_length);
            rt_kprintf("echo, uart6: tx: %ld, rx: %ld\r\n", u6tx_length, u6rx_length);
            if (u3tx_length != u3rx_length || u6tx_length != u6rx_length || u3tx_length != u6tx_length)
            {
                rt_kprintf("echo test error!!!\r\n");
                return;
            }

            // 前面已经判断过4个值是否相等了
            if (u3tx_length != sendTotalCount)
            {
                rt_kprintf("发送数据不对等,echo test error!!!\r\n");
                return;
            }
        }
    }
}

static void self_tx_rx_u2_thread_entry(void *parameter)
{
    static char rx_buffer[1024 + 1];
    rt_ssize_t size;
    int count = 0;
    while (1)
    {
         size = rt_device_read(u2serial, 0, rx_buffer, 1000);

        u2rx_length += size;
    }
}

static void self_tx_rx_u5_thread_entry(void *parameter)
{
    static char rx_buffer[1024 + 1];

    while (1)
    {
        u5rx_length += rt_device_read(u5serial, 0, rx_buffer, 500);
    }
}

/**
 * @brief 测试思路,创建u2和u5线程接收自己的数据并记录接收长度,在当前线程u2和u5发送数据,等待数据发送完毕,判断发送数据长度和接收数据长度是否一致
 *
 * @param parameter
 */
static void self_tx_rx_test(void *parameter)
{
    static char tx_buffer[1024 + 1];
    for (uint32_t i = 0; i < sizeof(tx_buffer); i++)
        tx_buffer[i] = i;

    char *uart_name = "uart2";
    u2serial = rt_device_find(uart_name);
    if (!u2serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }
    rt_device_open(u2serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    char *uart5_name = "uart5";
    u5serial = rt_device_find(uart5_name);
    if (!u5serial)
    {
        rt_kprintf("find %s failed!\n", uart5_name);
        return;
    }
    rt_device_open(u5serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_thread_startup(rt_thread_create("serial2", self_tx_rx_u2_thread_entry, RT_NULL, 2048, 6, 5));
    rt_thread_startup(rt_thread_create("serial5", self_tx_rx_u5_thread_entry, RT_NULL, 2048, 6, 5));

    for (uint32_t i = 0;; i++)
    {
        u2tx_length += rt_device_write(u2serial, 0, tx_buffer, 1000);
        u5tx_length += rt_device_write(u5serial, 0, tx_buffer, 500);

        rt_device_control(u2serial, RT_SERIAL_CTRL_TX_FLUSH, RT_NULL);
        rt_device_control(u5serial, RT_SERIAL_CTRL_TX_FLUSH, RT_NULL);

        // rt_thread_mdelay(10);

        if (i % 100 == 0)
        {
            rt_kprintf("self_test, uart2: tx: %ld, rx: %ld\r\n", u2tx_length, u2rx_length);
            rt_kprintf("self_test, uart5: tx: %ld, rx: %ld\r\n", u5tx_length, u5rx_length);
            if (u2tx_length != u2rx_length || u5tx_length != u5rx_length)
            {
                rt_kprintf("self test error!!!\r\n");
                return;
            }

            if (u5rx_length * 2 != u2rx_length)
            {
                rt_kprintf("数据不等长 self test error!!!\r\n");
                return;
            }
        }
    }
}

//
// uart_blocking_rx
// uart_blocking_tx
// testcases.drivers.uart_flush_rx
// testcases.drivers.uart_flush_txb
// testcases.drivers.uart_flush_txnb
// testcases.drivers.uart_get_unread_bytes_count
// uart_nonblocking_rx
// uart_nonblocking_tx
// testcases.drivers.uart_overflow_rxb_txb
// testcases.drivers.uart_rxb_txb
// testcases.drivers.uart_rxb_txnb
// testcases.drivers.uart_rxnb_txb
// testcases.drivers.uart_rxnb_txnb
// testcases.drivers.uart_timeout_rxb_txb

static int uart_test(void)
{
    rt_thread_startup(rt_thread_create("echo_test", echo_test, RT_NULL, 2048, 10, 5));

    rt_thread_startup(rt_thread_create("self_test", self_tx_rx_test, RT_NULL, 2048, 10, 5));
    return 0;
}

MSH_CMD_EXPORT_ALIAS(uart_test, uart_test, uart test);
ret

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/workflows/bsp_buildings.yml 详细请参考链接BSP自查

@github-actions github-actions bot added BSP BSP: WCH BSP related with WCH labels Jul 16, 2025
Copy link

github-actions bot commented Jul 16, 2025

📌 Code Review Assignment

🏷️ Tag: bsp_wch

Reviewers: Maihuanyi

Changed Files (Click to expand)
  • bsp/wch/risc-v/Libraries/ch32_drivers/SConscript
  • bsp/wch/risc-v/Libraries/ch32_drivers/drv_usart_v2.c
  • bsp/wch/risc-v/Libraries/ch32_drivers/drv_usart_v2.h
  • bsp/wch/risc-v/ch32v307v-r1/board/Kconfig

📊 Current Review Status (Last Updated: 2025-07-20 16:49 CST)

  • Maihuanyi Pending Review

📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@CYFS3
Copy link
Contributor Author

CYFS3 commented Jul 16, 2025

CI问题的,不知道为什么会默认启动CONFIG_BSP_USING_UART

@Ryan-CW-Code
Copy link
Contributor

楼主可以跑一下utest吗? 中断和dma都可以跑一遍看看
https://github.com/RT-Thread/rt-thread/blob/master/examples/utest/testcases/drivers/serial_v2/README.md

ci的问题,启用了串口功能但是没有使能硬件串口
image

@CYFS3
Copy link
Contributor Author

CYFS3 commented Jul 16, 2025

楼主可以跑一下utest吗? 中断和dma都可以跑一遍看看 https://github.com/RT-Thread/rt-thread/blob/master/examples/utest/testcases/drivers/serial_v2/README.md

ci的问题,启用了串口功能但是没有使能硬件串口 image

好的,但是除了devices.uart,其他还是会把BSP_USING_UART使能导致没有硬件串口,然后devices.uart确实是没有使能硬件串口

@Maihuanyi
Copy link
Contributor

楼主可以跑一下utest吗? 中断和dma都可以跑一遍看看 https://github.com/RT-Thread/rt-thread/blob/master/examples/utest/testcases/drivers/serial_v2/README.md
ci的问题,启用了串口功能但是没有使能硬件串口 image

好的,但是除了devices.uart,其他还是会把BSP_USING_UART使能导致没有硬件串口,然后devices.uart确实是没有使能硬件串口

你可以在CI里面启用硬件的串口的

@CYFS3 CYFS3 force-pushed the adapt_wch_risc_uartv2 branch from 0288f6a to 7de1d74 Compare July 20, 2025 08:07
@CYFS3
Copy link
Contributor Author

CYFS3 commented Jul 20, 2025

PixPin_2025-07-20_16-07-27 utest测试

@CYFS3 CYFS3 force-pushed the adapt_wch_risc_uartv2 branch from fdddb81 to 4d3d25f Compare July 20, 2025 08:44
@CYFS3 CYFS3 force-pushed the adapt_wch_risc_uartv2 branch from 4d3d25f to 561902a Compare July 20, 2025 08:49
@CYFS3
Copy link
Contributor Author

CYFS3 commented Jul 20, 2025

@Ryan-CW-Code @Maihuanyi

@CYFS3 CYFS3 requested a review from supperthomas July 20, 2025 08:56
@Maihuanyi
Copy link
Contributor

lgtm

@Ryan-CW-Code
Copy link
Contributor

lgtm

@supperthomas supperthomas merged commit 0441065 into RT-Thread:master Jul 21, 2025
37 checks passed
@CYFS3 CYFS3 deleted the adapt_wch_risc_uartv2 branch July 21, 2025 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BSP: WCH BSP related with WCH BSP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants